#include "GAF_Player.h"

namespace GAF
{
	UINT Player::m_nID = 0;
	UINT Player::GetID()
	{
		return m_nID++;
	}

	Player::Player()
	{
		static char _szPlayerName[20];
		_MEMSET(_szPlayerName, 0, sizeof(_szPlayerName));
		_SPRINTF(_szPlayerName, "P_%d", GetID());

		ATTRIB(PlayerName)	= _szPlayerName;
		ATTRIB(State)		= PLAYER_STOP;
		ATTRIB(Loop)		= false;
		m_pResponse		= NULL;

	}

	Player::~Player()
	{
	}

	void Player::SetPlayerName(_STRING strPlayerName)
	{
		if(ATTRIB(PlayerName).substr(0, 2) == "P_")
			ATTRIB(PlayerName) = strPlayerName;
	}

	void Player::Play(Bundle* pParam)
	{
		GAF_CHECK(ATTRIB(State) != PLAYER_PLAY);

		if(OnPlay(pParam))
		{
			if(Playing(pParam))
			{
				ATTRIB(State) = PLAYER_PLAY;
				if(m_pResponse != NULL)
				{
					m_pResponse->OnPlayerResponse(this, PLAYER_PLAY);
				}
			}
		}
	}

	void Player::Pause()
	{
		GAF_CHECK(ATTRIB(State) == PLAYER_PLAY);

		if(OnPause())
		{
			if(Pauseing())
			{
				ATTRIB(State) = PLAYER_PAUSE;
				if(m_pResponse != NULL)
				{
					m_pResponse->OnPlayerResponse(this, PLAYER_PAUSE);
				}
			}
		}
	}

	void Player::Stop()
	{
		GAF_CHECK(ATTRIB(State) != PLAYER_STOP);

		if(OnStop())
		{
			if(Stoping())
			{
				ATTRIB(State) = PLAYER_STOP;
				if(m_pResponse != NULL)
				{
					m_pResponse->OnPlayerResponse(this, PLAYER_STOP);
				}
			}
		}
	}

	void Player::SetResponse(PlayerResponse* pResponse)
	{
		GAF_CHECK(pResponse != NULL);

		m_pResponse = pResponse;
	}
}
